package com.yamaha.bikedesalon.postItem;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.json.JSONObject;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yamaha.bikedesalon.R;

public class BodyCommentItem implements Item {

	// ============================================================
	private static class ItemHolder {
		ImageView icon;
		TextView comment;
		TextView commentTime;
	}

	// ============================================================

	private JSONObject commentInfo = null;
	private final ImageLoader imageLoader;

	public BodyCommentItem(ImageLoader imageLoader, JSONObject object) {
		this.imageLoader = imageLoader;
		commentInfo = object;
	}

	@Override
	public JSONObject getObject() {
		return commentInfo;
	}

	@Override
	public int getViewType() {
		return ViewType.ITEM.ordinal();
	}

	@Override
	public View getView(LayoutInflater inflater, View convertView, int position) {
		ItemHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.body_comment_item, null);
			convertView.setId(0x2033 + position);

			holder = new ItemHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.comment_user_image);
			holder.comment = (TextView) convertView.findViewById(R.id.comment_text);
			holder.commentTime = (TextView) convertView.findViewById(R.id.comment_datetime);

			convertView.setTag(holder);
		} else {
			holder = (ItemHolder) convertView.getTag();

		}

		// change bg
		// if (position % 2 == 0) {
		// // change bg
		// convertView.setBackgroundResource(R.drawable.comment_bg1);
		// } else {
		// convertView.setBackgroundResource(R.drawable.comment_bg2);
		// }

		// fill each data
		try {

			JSONObject userInfo = commentInfo.getJSONObject("user");

			final String icon = userInfo.getString("profile_image");
			if (icon == null || icon.length() == 0) {
				holder.icon.setImageBitmap(null);
			} else {
				imageLoader.displayImage(icon, holder.icon);
			}

			holder.comment.setText(commentInfo.getString("comment"));

			Date date = new Date(Long.getLong(commentInfo.getString("last_update")) / 1000);

			final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.UK);

			holder.commentTime.setText(dateFormat.format(date));

		} catch (Exception e) {
			Log.e("Comment BodyI tem View Error", e.getMessage(), e);
		}

		return convertView;
	}
}
